﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Net;
using System.Data.SQLite;
using Com.Toook.Common;
using Com.Toook.Images;

public partial class EditDownload_Movie_DownloadMovieImg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ButtonDownload_Click(object sender, EventArgs e)
    {
        if(!Page.IsValid)
        {
            LabelMsg.Text = "输入数据不合法";
            return;
        }
        string savePath = Server.MapPath("~\\DownloadImgs\\Movie\\xddy\\");        
        int startId = int.Parse(TextBoxStartId.Text);
        int endId = int.Parse(TextBoxEndId.Text);
        string prefix = TextBoxImgPrefix.Text.Trim();

        WebClient webClientImg = new WebClient();
        for(int i = startId; i <= endId; i++)
        {
            string imgNameOriginal = prefix + i.ToString("D5") + "_original";//本地原始图片名称
            string imgNameResize = prefix + i.ToString("D5");//本地调整大小后的图片名称
            string img = GetSourceImg(i.ToString());
            int dot = -1;
            if(img.Length > 5)
            {
                dot = img.LastIndexOf('.');
            }
            if(dot == -1)
            {
                continue;
            }
            string ext = img.Substring(dot);
            try
            {
                //1、从原网站下载图片，按指定的文件名保存。
                webClientImg.DownloadFile(img, savePath + imgNameOriginal + ext);
            }
            catch(Exception ex)
            {
                LabelMsg.Text += "下载图片出错。Id=" + i.ToString() + ", img=" + img + ex.Message + "<br /><br />";
                continue;
            }
            //2、调整图片大小
            ImageHelper.MakeThumbnail(savePath + imgNameOriginal + ext,
                savePath + imgNameResize + ext, 
                DropDownListMode.SelectedValue, 
                int.Parse(TextBoxWidth.Text), 
                int.Parse(TextBoxHeight.Text) );
        }
        LabelMsg.Text = "下载图片完成。" + TextBoxStartId.Text + "到" + TextBoxEndId.Text;
    }
    /// <summary>
    /// 根据id获取电影来源图片地址
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    private string GetSourceImg(string id)
    {
        string sql = "select img from " + TextBoxTableName.Text + " where id = " + id;
        SQLiteConnection conn = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmd = new SQLiteCommand(sql, conn);
        string img = "";
        try
        {
            conn.Open();
            img = cmd.ExecuteScalar().ToString();
        }
        finally
        {
            SqliteHelper.CloseDbResource(conn, null);
        }
        return img;
    }   
    
}
